package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.user.activity.ConsumptionRecordActivity;
import com.asktgapp.user.activity.SystemMessageActivity;
import com.asktgapp.modulebase.common.util.PreferencesUtil;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/8
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description: 消息中心fragment
 */

public class MessageCenterFragment extends BaseFragment {
    @InjectView(R.id.tv_msg_center_message)
    TextView mSystemMessage;
    @InjectView(R.id.tv_msg_center_consume)
    TextView mCoustomRecord;
    private double unRead = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_message_center, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mSystemMessage.setOnClickListener(this);
        mCoustomRecord.setOnClickListener(this);
    }

    @Override
    public void loadData() {
//        String userid = PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID);
//        Apiservice apiservice = ApiUtil.getInstance().create();
//        HashMap params = new HashMap();
//        params.put("userid", userid);
//        Call<ApiResponseBody> call = apiservice.getNotReadMsg(params);
//        call.enqueue(new Callback<ApiResponseBody>() {
//            @Override
//            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
////                if(getActivity().isFinishing())
////                    return;
//                if (response.isSuccessful()) {
//                    if (response.body().getResult() != null)
//                        unRead = (double) response.body().getResult();
//                    if (unRead > 0) {
//                        mSystemMessage.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ico_sys_unread, 0, R.mipmap.icon_more, 0);
//                    } else {
//                        mSystemMessage.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ico_system_message, 0, R.mipmap.icon_more, 0);
//                    }
//                } else {
//                    showTost(response.raw().message(), 0);
//                }
//            }
//
//            @Override
//            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
//                ApiException apiException = ExceptionHandle.handleException(t);
//                showTost(apiException.getMessage(), 1);
//
//            }
//        });
//        addSubscription(call);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_msg_center_consume:
                startActivity(new Intent(getActivity(), ConsumptionRecordActivity.class));
                break;
            case R.id.tv_msg_center_message:
                startActivity(new Intent(getActivity(), SystemMessageActivity.class));
                break;
        }


    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }
}
